using System;
using System.Collections;
using PrimeTween;
using UnityEngine;

namespace Actions
{
    public class PlayerChaser : MonoBehaviour
    {
        public static event Action AnyTargetHitPlayer;
        
        [SerializeField] private float minChaseDistance = 1f;
        [SerializeField] private float disappearTime = 4f;
        
        private Mover _mover;
        private Collider2D _collider2D;

        private Transform _player;


        private void Start()
        {
            _mover = GetComponent<Mover>();
            _collider2D = GetComponentInChildren<Collider2D>();

            _player = GameObject.FindGameObjectWithTag("Player").transform;
            
            Vector3 targetPosition = _player.position;
            Vector3 moveDirection = targetPosition - transform.position;

            _mover.Move(moveDirection.normalized);
        }

        private void OnTriggerEnter2D(Collider2D other)
        {
            OnHitPlayer();
        }

        private void OnHitPlayer()
        {
            enabled = false;
            _collider2D.enabled = false;
            _mover.StopMoving();
            AnyTargetHitPlayer?.Invoke();

            Tween.Scale(transform, 0f, disappearTime)
                .OnComplete(DestroySelf, false);
        }

        private void DestroySelf()
        {
            Destroy(gameObject);
        }
    }
}